using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DataTransferObject;

namespace DataAccess
{
    public class clsTheLoaiDAO:clsDataAccessHelper
    {
        //Fields.
        private String tenTable;

        //Constructors.
        public clsTheLoaiDAO()
        {
            this.tenTable = "THELOAI";
        }

        #region Methods
        //----------------------------------------------------------------------------------------------------------//
        //Lấy dữ liệu bảng.
        public DataTable SelectAll()
        {
            String sql;
            DataTable dt;

            try
            {
                sql = "SELECT * FROM "+this.tenTable;
                dt = new DataTable();
                dt = this.ExecuteQuery(sql);

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable ThongKeTheoTheLoai(clsTheLoaiDTO obj)
        {
            string sql;
            DataTable tbl;
            try
            {
                sql ="select DKCB,TenTuaSach,Gia,TenTL from TheLoai,TuaSach,Sach where TuaSach.MaTL=TheLoai.MaTL and TuaSach.MaTuaSach=Sach.MaTuaSach and TuaSach.MaTL='" + obj.MaTL + "'";
                tbl = new DataTable();
                tbl = this.ExecuteQuery(sql);
                return tbl;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //----------------------------------------------------------------------------------------------------------//
        //Xóa 1 record trong bảng.
        public bool Delete(clsTheLoaiDTO theLoai)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "DELETE ";
                sql += " FROM " + this.tenTable;
                sql += " WHERE MATL LIKE '" + theLoai.MaTL + "'";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }

        //----------------------------------------------------------------------------------------------------------//
        //Thêm 1 record vào bảng.
        public bool Insert(clsTheLoaiDTO theLoai)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "INSERT INTO " + this.tenTable;
                sql += " values('";
                sql += theLoai.MaTL + "',N'";
                sql += theLoai.TenTL + "')";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }

        //----------------------------------------------------------------------------------------------------------//
        //Sửa 1 record trong bảng.
        public bool Update(clsTheLoaiDTO theLoai)
        {
            String sql;
            bool check = false;

            try
            {
                sql = "UPDATE " + this.tenTable;
                sql += " SET";
                sql += " TenTL = N'";
                sql += theLoai.TenTL + "'";
                sql += " WHERE MATL LIKE '";
                sql += theLoai.MaTL + "'";
                check = this.ExecuteNonQuery(sql);

                return check;
            }
            catch
            {
                return check;
            }
        }
        #endregion
    }
}
